layui.define(['form', 'laydate', 'table', 'userApi'], function (exports) {
    var form = layui.form;
    var laydate = layui.laydate;
    var table = layui.table;
    var userApi = layui.userApi;
    var userTable = null;

    var view = {

        init: function () {
            this.initTable();
            this.initSearchForm();
            this.initToolBar();
            window.dataReload = function () {
                Lib.doSearchForm($("#searchForm"), userTable)
            }
        },

        initTable: function () {
            userTable = table.render({
                elem: '#userTable',
                height: Lib.getTableHeight(),
                method: 'post',
                url: Common.ctxPath + '/cms/employeeManager/list.json' //数据接口
                , page: Lib.tablePage //开启分页
                , limit: 10,
                cols: [[ //表头
                    {
                        type: 'checkbox',
                        fixed: 'left',
                    },  {
                        title: '序号',
                        type: 'numbers'
                    }, {
                        field: 'code',
                        title: '登录账号',
                        align: 'center',
                        width:"200"
                    }, {
                        field: 'name',
                        title: '用户姓名',
                        align: 'center',
                        width:"200"
                    }, {
                        field: 'stateText',
                        title: '状态',
                        align: 'center',
                        width:"135"
                    }
                    , {
                        field: 'roleName',
                        title: '用户类型',
                        align: 'center',
                        width:"135"
                    }
                ]]

            });
        },

        initSearchForm: function () {
            Lib.initSearchForm($("#searchForm"), userTable, form);
        },
        initToolBar: function () {
            toolbar = {
                add: function () { //获取选中数据
                    var url = "/cms/employeeManager/add.do";
                    Common.openDlg(url, "用户管理>新增");
                },
                edit: function () { //获取选中数目
                    var data = Common.getOneFromTable(table, "userTable");
                    if (data == null) {
                        return;
                    }
                    var url = "/cms/employeeManager/edit.do?id=" + data.id;
                    Common.openDlg(url, "用户管理>编辑");

                },
                del: function () {
                    Common.openConfirm("确认要删除这些用户?", function () {
                        layui.use(['del'], function () {
                            var delView = layui.del;
                            delView.delBatch();
                        });
                    })
                },
                disable: function () {
                    var data = Common.getMoreDataFromTable(table, "userTable");
                    if (data == null) {
                        return;
                    }
                    Common.openConfirm("确认要禁用这些用户?", function () {
                        var ids = Common.concatBatchId(data);
                        Common.post("/cms/employeeManager/disable.json", {
                            "ids": ids
                        }, function () {
                            Common.info("禁用成功");
                            dataReload();
                        })
                    })
                },
                enable: function () {
                    var data = Common.getMoreDataFromTable(table, "userTable");
                    if (data == null) {
                        return;
                    }
                    Common.openConfirm("确认要启用这些用户?", function () {
                        var ids = Common.concatBatchId(data);
                        Common.post("/cms/employeeManager/enable.json", {
                            "ids": ids
                        }, function () {
                            Common.info("启用成功");
                            dataReload();
                        })
                    })
                },

                changePassword: function () {
                    var data = Common.getOneFromTable(table, "userTable");
                    if (data == null) {
                        return;
                    }
                    var url = "/cms/employeeManager/changePassword.do?id=" + data.id;
                    Common.openDlg(url, "重置密码");
                },
                checkPassword: function () {
                    var data = Common.getOneFromTable(table, "userTable");
                    if (data == null) {
                        return;
                    }
                    Common.openDlg("/jsPrint.html?id=" + data.id, "密码函");
                }
            };
            $('.ext-toolbar').on('click', function () {
                var type = $(this).data('type');
                toolbar[type] ? toolbar[type].call(this) : '';
            });
        }
    }

    exports('index', view);

});